using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace PointBridge.SPDX
{
    public class Web
    {
        public static void Alert(Page page, string msg)
        {
            if (page.ClientScript.IsStartupScriptRegistered("Alert") != true)
            {
                msg = msg.Replace("'", " ").Replace("\n", "\\n").Replace("\r", "");
                page.ClientScript.RegisterStartupScript(page.GetType(), "Alert", "<script>alert('" + msg + "'); </script>");
            }
        }
        public static void AlertAndGo(Page page, string msg, string url)
        {
            if (page.ClientScript.IsStartupScriptRegistered("Alert") != true)
            {
                msg = msg.Replace("'", " ").Replace("\n", "\\n").Replace("\r", "");
                string script = "<script>" +
                                "alert('" + msg + "');" +
                                "window.location.href ='" + url + "'" +
                                "</script>";

                page.ClientScript.RegisterStartupScript(page.GetType(), "Alert", script);
            }
        }
        public static void ConfirmAndGo(Page page, string msg, string url)
        {
            if (page.ClientScript.IsStartupScriptRegistered("Question") != true)
            {
                msg = msg.Replace("'", " ").Replace("\n", "\\n").Replace("\r", "");
                string script = "<script>" +
                                "if (window.confirm('" + msg + "') == true) window.location.href ='" + url + "'" +
                                "</script>";

                page.ClientScript.RegisterStartupScript(page.GetType(), "Question", script);
            }
        }
        public static void Go(Page page, string url)
        {
            if (page.ClientScript.IsStartupScriptRegistered("Alert") != true)
            {
                string script = "<script>" +
                    "window.location.href ='" + url + "'" +
                    "</script>";

                page.ClientScript.RegisterStartupScript(page.GetType(), "Alert", script);
            }
        }
        //---------------------------------------------------------------------------------------------------------------------------------------
        public static void MarkError(Page page, string varId)
        {
            if (varId.Length == 0) return;
            Control c = page.FindControl(varId);
            if (c != null)
            {
                if (c.GetType() == typeof(TextBox)) MarkError((TextBox)c);
                if (c.GetType() == typeof(DropDownList)) MarkError((DropDownList)c);
                if (c.GetType() == typeof(CheckBox)) MarkError((CheckBox)c);
                if (c.GetType() == typeof(ListBox)) MarkError((ListBox)c);
                if (c.GetType() == typeof(RadioButtonList)) MarkError((RadioButtonList)c);
            }
        }
        public static void MarkError(RadioButtonList wRadioButtonList)
        {
            //wCheckBox.ForeColor = Color.White;
            //wCheckBox.BackColor = Color.FromArgb(0xff, 0x38, 0x00);
            wRadioButtonList.Attributes["style"] = "color:White;background-color:#FF3800;";

            Focus(wRadioButtonList);
        }
        public static void MarkError(ListBox wListBox)
        {
            //wCheckBox.ForeColor = Color.White;
            //wCheckBox.BackColor = Color.FromArgb(0xff, 0x38, 0x00);
            wListBox.Attributes["style"] = "color:White;background-color:#FF3800;";

            Focus(wListBox);
        }
        public static void MarkError(CheckBox wCheckBox)
        {
            //wCheckBox.ForeColor = Color.White;
            //wCheckBox.BackColor = Color.FromArgb(0xff, 0x38, 0x00);
            wCheckBox.Attributes["style"] = "color:White;background-color:#FF3800;";

            Focus(wCheckBox);
        }
        public static void MarkError(TextBox wTextBox)
        {
            //wTextBox.ForeColor = Color.White;
            //wTextBox.BackColor = Color.FromArgb(0xff, 0x38, 0x00);
            wTextBox.Attributes["style"] = "color:White;background-color:#FF3800;";

            Focus(wTextBox);
            Highlight(wTextBox);
        }
        public static void MarkError(DropDownList wDropDownList)
        {
            //wDropDownList.ForeColor = Color.White;
            //wDropDownList.BackColor = Color.FromArgb(0xff, 0x38, 0x00);
            wDropDownList.Attributes["style"] = "color:White;background-color:#FF3800;";

            Focus(wDropDownList);
        }
        //---------------------------------------------------------------------------------------------------------------------------------------
        public static void Highlight(Page page, string varId)
        {
            if (varId.Length == 0) return;
            if (page.ClientScript.IsStartupScriptRegistered("Highlight") != true)
            {
                string script = "<script>" +
                                "if (window.navigator.appName.toLowerCase().indexOf('netscape') <= -1)" +
                                "{" +
                                "	document.Form1." + ToClientFormat(varId) + ".select();" +
                                "}" +
                                "</script>";
                page.ClientScript.RegisterStartupScript(page.GetType(), "Highlight", script);
            }
        }
        public static void Highlight(TextBox wTextBox)
        {
            Highlight(wTextBox.Page, wTextBox.UniqueID);
        }
        //---------------------------------------------------------------------------------------------------------------------------------------
        public static void Focus(Page page, string varId)
        {
            if (varId.Length == 0) return;
            if (page.ClientScript.IsStartupScriptRegistered("Focus") != true)
            {
                string script = "if (window.navigator.appName.toLowerCase().indexOf('netscape') <= -1)" +
                                "{" +
                                "	document.Form1." + ToClientFormat(varId) + ".focus();" +
                                "}";
                page.ClientScript.RegisterStartupScript(page.GetType(), "Focus", script, true);
            }
        }
        public static void Focus(RadioButtonList wRadioButtonList)
        {
            if (wRadioButtonList.Items.Count >= 1) Focus(wRadioButtonList.Page, wRadioButtonList.UniqueID + "_0");
        }
        public static void Focus(TextBox wTextBox)
        {
            Focus(wTextBox.Page, wTextBox.UniqueID);
        }
        public static void Focus(DropDownList wDropDownList)
        {
            Focus(wDropDownList.Page, wDropDownList.UniqueID);
        }
        public static void Focus(CheckBox wCheckBox)
        {
            Focus(wCheckBox.Page, wCheckBox.UniqueID);
        }
        public static void Focus(ListBox wListBox)
        {
            Focus(wListBox.Page, wListBox.UniqueID);
        }
        public static void Focus(Button wButton)
        {
            Focus(wButton.Page, wButton.UniqueID);
        }
        //---------------------------------------------------------------------------------------------------------------------------------------
        public static void ScrollTo(Page page, string elementId)
        {
            if (page.ClientScript.IsStartupScriptRegistered("ScrollTo") != true)
            {
                string script = "<script>" + "\n" +
                                "function ScrollToElement(){" + "\n" +
                                "var theElement = document.getElementById('" + elementId + "');" + "\n" +
                                "var selectedPosX = 0;" + "\n" +
                                "var selectedPosY = 0;" + "\n" +
                                "while(theElement != null){" + "\n" +
                                "	selectedPosX += theElement.offsetLeft;" + "\n" +
                                "	selectedPosY += theElement.offsetTop;" + "\n" +
                                "	theElement = theElement.offsetParent;" + "\n" +
                                "}" + "\n" +
                                "window.scrollTo(selectedPosX,selectedPosY);" + "\n" +
                                "</script>" + "\n";
                page.ClientScript.RegisterStartupScript(page.GetType(), "ScrollTo", script);
            }
        }
        public static void ScrollTo(WebControl wCtrl)
        {
            ScrollTo(wCtrl.Page, wCtrl.UniqueID);
        }
        //---------------------------------------------------------------------------------------------------------------------------------------
        public static string ToClientFormat(string name)
        {
            return name.Replace(":", "_");
        }
    }
}
